#ifndef __MYSQL_NET__
#define __MYSQL_NET__

#include <stdint.h>
#include <sys/time.h>
#include <string>
#include <map>

#include "reactor_handler.h"
#include "tcp_handle_base.h"
#include "svr_reactor_facade.h"
#include "thread.h"
#include "const.h"
#include "context.h"
#include "mysql_pkg.h"

class CMysqlNet
: public ITimerHandler
, public ITCPPacketHandleSink
, public snda::comm::thread::CThreadBase
{
public:
    CMysqlNet();
    ~CMysqlNet();  
public:
    int Start();
	void End();
	CReactor* GetReactor();
    virtual int OnConnect(
        CTcpHandleBase* pTcpHandleBase,
        const char* pszPeerIP,
        unsigned short wPeerPort);
	/*
	 *	Recv a complete PDU
	 */
    virtual int OnRecv(
        CTcpHandleBase* pTcpHandleBase,
        char* pBuffer,
        uint32_t nBufLen,
        uint32_t nLeft);

	/*
	 *	Recv a break PDU
	 */
    virtual int OnBreakRecv(
        CTcpHandleBase* pTcpHandleBase);

	virtual int OnSend(
		CTcpHandleBase* pTcpHandleBase);
    virtual int OnClose(
        CTcpHandleBase* pTcpHandleBase,
        int iRetCode);

private:
    virtual int OnTimer(
        int iTimerID);

    int initServer();
    int fini();
	virtual void* Run();
	void DoSwitch(bool bForce=false);
	bool DoCheckAcl(CTcpHandleBase* pTcpHandle);
	void DoResetAllConns();
	void DoResetTimeoutQuery();
private:
    bool 		m_bInit;
    CReactor* 	m_pReactor;
    int 		m_iCheckTimerId; 
	int			m_iLoadConfTimerId;
	Context* 	m_pCtx;
	Mysql*		m_pMysql;
	uint32_t 	m_dwConnNum;
	CMysqlPkg*	m_pMyPkg;
    CTcpHandleBase* m_pTCPNetHandler;
	char*		m_pBuff;
	int			m_iBuffSize;
	int			m_iToken;
	int			m_iTimer;
	int*		m_arrIdxs;
	RingBuckets*	m_pConnBuks;
	RingBuckets*	m_pQueryBuks;
    CTcpHandleBase** m_pAsyncConn;
	DB*			m_pWorkDB;
	MemPiece*	m_pMemPiece;
	int			m_iTemp;
};
 
#endif 
